package com.stone.business.jc.action;

/**
 * <pre>
 * 专项考核记录
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import com.stone.business.jc.pojo.TZxkhbz;
import com.stone.business.jc.service.JcCommonService;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.page.CustomPage;

public class ZxkhbzAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;
	
	private String start;
	private String limit;
	
	private String bzid;
	
	private String id;

	private JcCommonService myService;
	
	private TZxkhbz entity;

	public void setMyService(JcCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public ZxkhbzAction() {
        this.entityClass = TZxkhbz.class;
    }
    
    /**
     * 初始化进入
     */
    public String initDetail() throws Exception {
    	
    	// 考勤日期 默认上个月
    	List<HashMap<String,Object>> list = myService.findListBySql("SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS NOWMONTH  FROM DUAL");
    	
		super.getSession().setAttribute("KQRQ", list.get(0).get("nowmonth"));
		
		return SUCCESS;
    }
    
    /**
     * 保存
     * @throws Exception
     */
    public void save() throws Exception {
    	
		// 修改直接修改
		if (entity.getId() != null && entity.getId() != 0) {
			
			// 是否修改为其他已有的日期
			Long count = myService.getTotalByDHql("khrq = ? and bzid = ? and id != ?", new Object[]{entity.getKhrq(), entity.getBzid(),entity.getId()}, "TZxkhbz");
			
			if (count == 0) {
				myService.update(entity);
			}
				
			// 新建要判断字典是否重复
		} else {
		
			// 判断是否已经有了 字典类型
			Long count = myService.getTotalByDHql("khrq = ? and bzid = ? ", new Object[]{entity.getKhrq(), entity.getBzid()}, "TZxkhbz");
			
			if (count > 0 ) {
				
				myService.updateByHQL("update TZxkhbz set fsyy = ? , fs = ? where khrq = ? and bzid = ? ", new Object[]{entity.getFsyy(), entity.getFs(), entity.getKhrq(), entity.getBzid()});
			} else {

				myService.save(entity);
			}
			
		}
    	
    	
//    	if (entity.getId() == 0) {
//    		myService.save(entity);
//    	} else {
//    		myService.update(entity);
//    	}
    	
        super.getResponse().getWriter().print("{msg:'保存成功'}");
    	
    }
    
    /**
     * 删除
     * @throws Exception
     */
    public void delete() throws Exception {
    	
    	myService.delete(Long.parseLong(id), "TZxkhbz");
    	
        super.getResponse().getWriter().print("{msg:'删除成功'}");
    	
    }
    
	/**
	 * 用户的出勤
	 * 
	 * @return
	 * @throws AppException
	 */
	public void getListExt() throws Exception {

		super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		String cols = " kq.id as id, kq.bzid as bzid, kq.khrq as khrq, kq.fsyy as fsyy, kq.fs as fs";

		m.put("cols", cols);
		m.put("alias", "kq");
		m.put("join", "");
	
		String conditions = " kq.bzid = '" + bzid + "'";// '"+super.getSearchBean().getCon1()+"'");

		m.put("conditions",conditions);//conditions);
		m.put("clause", " order by kq.khrq desc");
		
		// 根据起始记录查询当前页记录
		List<HashMap<String,Object>> list = myService.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()) , m,this.entityClass.getName() );
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strTree = "{result:" + json.toString() + ",totalCount:[" + service.getTotalByDHql(conditions, null, this.entityClass.getName() + " as kq"  ) + "]}";
		
        super.getResponse().getWriter().print(strTree);
		
	}

	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit == null ? String.valueOf(CustomPage.PER_PAGE) : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getBzid() {
		return bzid;
	}

	public void setBzid(String bzid) {
		this.bzid = bzid;
	}

	@Override
	public HashMap<String, Object> setSearchCondition() {
		// TODO Auto-generated method stub
		return null;
	}

	public TZxkhbz getEntity() {
		return entity;
	}

	public void setEntity(TZxkhbz entity) {
		this.entity = entity;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	
}
